package com.jing.ads.downloader;

import com.jing.ads.R;
import com.jing.ads.application.MyApplication;
import com.jing.ads.handler.LogHandler;
import com.jing.ads.net.RequestListener;
import com.jing.ads.util.NotificationUtil;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class DownloadInfo {
	public int id;
    public String uri;
    public boolean noIntegrity;
    public String hint;
    public String filename;
    public String mimetype;
    public int destination;
    public int visibility;
    public int control;
    public int status;
    public int numFailed;
    public int retryAfter;
    public int redirectCount;
    public long lastMod;
//    public String pckg;
//    public String clazz;
    public String extras;
    public String cookies;
    public String userAgent;
    public String referer;
    public int totalBytes;
    public int currentBytes;
    public String etag;
    public String title;
    //public boolean mediaScanned;

    public volatile boolean hasActiveThread;
    
	public DownloadInfo() {

	}
    
    public DownloadInfo(int id, String uri, boolean noIntegrity,
            String hint, String filename,
            String mimetype, int destination, int visibility, int control,
            int status, int numFailed, int retryAfter, int redirectCount, long lastMod,
            /*String pckg, String clazz,*/ String extras, String cookies,
            String userAgent, String referer, int totalBytes, int currentBytes, String etag, String title//,
            /*boolean mediaScanned*/) {
        this.id = id;
        this.uri = uri;
        this.noIntegrity = noIntegrity;
        this.hint = hint;
        this.filename = filename;
        this.mimetype = mimetype;
        this.destination = destination;
        this.visibility = visibility;
        this.control = control;
        this.status = status;
        this.numFailed = numFailed;
        this.retryAfter = retryAfter;
        this.redirectCount = redirectCount;
        this.lastMod = lastMod;
//        this.pckg = pckg;
//        this.clazz = clazz;
        this.extras = extras;
        this.cookies = cookies;
        this.userAgent = userAgent;
        this.referer = referer;
        this.totalBytes = totalBytes;
        this.currentBytes = currentBytes;
        this.etag = etag;
        this.title = title;
        //this.mediaScanned = mediaScanned;
    }
    
    public void sendIntentIfRequested(Uri contentUri, Context context) {
//        if (pckg != null && clazz != null) {
//            Intent intent = new Intent(Downloads.DOWNLOAD_COMPLETED_ACTION);
//            intent.setClassName(pckg, clazz);
//            if (extras != null) {
//                intent.putExtra(Downloads.COLUMN_NOTIFICATION_EXTRAS, extras);
//            }
//            // We only send the content: URI, for security reasons. Otherwise, malicious
//            //     applications would have an easier time spoofing download results by
//            //     sending spoofed intents.
//            intent.setData(contentUri);
//            context.sendBroadcast(intent);
//        }
    	
    	LogHandler lh = new LogHandler(context);
    	lh.setParams(LogHandler.ACTION_INSTALL, extras);
    	lh.onRequest(new RequestListener() {
			
			@Override
			public void onError(Object error) {
				
			}
			
			@Override
			public void onCallBack(Object data) {
				
			}
		}, false);
    	
    	MyApplication app = (MyApplication) context.getApplicationContext();
    	if (!app.isShowView()) {
    		String content = context.getString(R.string.notify_install, this.title);
    		NotificationUtil.getInstance(context).showNotify(this.title, content, extras, true);
			return;
		}
    	
    	if (this.extras != null && this.filename != null) {
			Intent i = new Intent("com.jing.ads.notify.complete");
			i.putExtra("pName", extras);
			i.putExtra("file", filename);
			context.sendBroadcast(i);
		}
    	
    }
    
    /**
     * Returns the time when a download should be restarted. Must only
     * be called when numFailed > 0.
     */
    public long restartTime() {
        if (retryAfter > 0) {
            return lastMod + retryAfter;
        }
        return lastMod +
                Constants.RETRY_FIRST_DELAY *
                    (1000 + Helpers.rnd.nextInt(1001)) * (1 << (numFailed - 1));
    }
    
    /**
     * Returns whether this download (which the download manager hasn't seen yet)
     * should be started.
     */
    public boolean isReadyToStart(long now) {
        if (control == Downloads.CONTROL_PAUSED) {
            // the download is paused, so it's not going to start
            return false;
        }
        if (status == 0) {
            // status hasn't been initialized yet, this is a new download
            return true;
        }
        if (status == Downloads.STATUS_PENDING) {
            // download is explicit marked as ready to start
            return true;
        }
        if (status == Downloads.STATUS_RUNNING) {
            // download was interrupted (process killed, loss of power) while it was running,
            //     without a chance to update the database
            return true;
        }
        if (status == Downloads.STATUS_RUNNING_PAUSED) {
            if (numFailed == 0) {
                // download is waiting for network connectivity to return before it can resume
                return true;
            }
            if (restartTime() < now) {
                // download was waiting for a delayed restart, and the delay has expired
                return true;
            }
        }
        return false;
    }
    
    /**
     * Returns whether this download (which the download manager has already seen
     * and therefore potentially started) should be restarted.
     *
     * In a nutshell, this returns true if the download isn't already running
     * but should be, and it can know whether the download is already running
     * by checking the status.
     */
    public boolean isReadyToRestart(long now) {
        if (control == Downloads.CONTROL_PAUSED) {
            // the download is paused, so it's not going to restart
            return false;
        }
        if (status == 0) {
            // download hadn't been initialized yet
            return true;
        }
        if (status == Downloads.STATUS_PENDING) {
            // download is explicit marked as ready to start
            return true;
        }
        if (status == Downloads.STATUS_RUNNING_PAUSED) {
            if (numFailed == 0) {
                // download is waiting for network connectivity to return before it can resume
                return true;
            }
            if (restartTime() < now) {
                // download was waiting for a delayed restart, and the delay has expired
                return true;
            }
        }
        return false;
    }

    /**
     * Returns whether this download has a visible notification after
     * completion.
     */
    public boolean hasCompletionNotification() {
        if (!Downloads.isStatusCompleted(status)) {
            return false;
        }
        if (visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
            return true;
        }
        return false;
    }

    /**
     * Returns whether this download is allowed to use the network.
     */
    public boolean canUseNetwork(boolean available, boolean roaming) {
        if (!available) {
            return false;
        }
        if (destination == Downloads.DESTINATION_CACHE_PARTITION_NOROAMING) {
            return !roaming;
        } else {
            return true;
        }
    }
}
